#!/home/pathop/anaconda3/bin/python

#get files generation time
import glob
import time
import calendar

import numpy as np
import pandas as pd

from datetime import datetime, timedelta

import os




#suppose handel predict output
#wrfout_predict_path="/lun01/pathop/wrffc/data/wrf/"
wrfout_predict_path="/public2/home/pathop/wrffc/wrf/outputs/"

#get UTC time

UTC_time_now=datetime.utcnow()

"'using [delta_time] to judge file folder'"
delta_time=timedelta(days=1,hours=6)
delta_time2=timedelta(days=2,hours=0)
delta_time1=timedelta(days=0,hours=18)

#delta_local=timedelta(hours=8)    #北京时间间隔

yesterday_time=UTC_time_now-delta_time
before_yesterday_time=UTC_time_now-delta_time2
yesterday_nearly_time=UTC_time_now-delta_time1

beijing_time_now=datetime.now()#get beijing time

#get the concrete time
yesterday_year=yesterday_time.year
yesterday_month=yesterday_time.month
yesterday_day=yesterday_time.day
yesterday_hour=yesterday_time.hour
yesterday_minu=yesterday_time.minute
before_yesterday_day=before_yesterday_time.day
yesterday_nearly_day=yesterday_nearly_time.day

str_year='%04d'%yesterday_year
str_mon='%02d'%yesterday_month
str_day='%02d'%(yesterday_day) #get yesterday time
str_before_day='%02d'%(before_yesterday_day) #get yesterday time
str_nearly_day='%02d'%(yesterday_nearly_day)




structure1=str_year+'/'+str_year+str_mon+'/'+str_year+str_mon+str_day+'12'+'/'
structure2=str_year+'/'+str_year+str_mon+'/'+str_year+str_mon+str_before_day+'12'+'/'
structure3=str_year+'/'+str_year+str_mon+'/'+str_year+str_mon+str_nearly_day+'12'+'/'
dir_exist1=os.path.isdir(wrfout_predict_path+structure1)
dir_exist2=os.path.isdir(wrfout_predict_path+structure2)
dir_exist3=os.path.isdir(wrfout_predict_path+structure3)

if dir_exist1:
    pass
else:
    structure1=structure2
if dir_exist2:
    pass
else:
    structure2=None
if dir_exist3:
    pass
else:
    structure3=None
if (structure1==None) & (structure2==None) & (structure3==None):
    print('path error')



wrf_file_title='wrfout_d02_*'
if (structure1!=None):
    wrfout_input_path1=wrfout_predict_path+structure1+wrf_file_title
    wrfout_files1=glob.glob(wrfout_input_path1)
elif (structure2!=None):
    wrfout_input_path2=wrfout_predict_path+structure2+wrf_file_title
    wrfout_files2=glob.glob(wrfout_input_path2)
    #wrfout_files=wrfout_files2
elif (structure3!=None):
    wrfout_input_path3=wrfout_predict_path+structure3+wrf_file_title
    wrfout_files3=glob.glob(wrfout_input_path3)
    #wrfout_files=wrfout_files3



wrfout_files=wrfout_files1
structure=structure1

if wrfout_files1==[]:
    wrfout_files=wrfout_files2
    structure=structure2
elif wrfout_files2==[]:
    wrfout_files=wrfout_files3
    structure=structure3
elif wrfout_files3==[]:
    print('error does not found wrf file')

wrfout_files=sorted(wrfout_files)


yesterday_UTC_12h=datetime(yesterday_year, yesterday_month, yesterday_day, 12)
#UTC_12h==Beijing_20h
#都是UTC时间
"'item is the order number'"
for item,wrf_file in enumerate(wrfout_files):
    
    postfix=wrf_file[-3:]
    option={'True':wrf_file[-22:-3],'False':wrf_file[-19:]}
    str_time=option[str(postfix=='.nc')]
    #print(str_time)
    try:
        wrf_file_time=datetime.strptime(str_time, '%Y-%m-%d_%H:%M:%S')
        wrf_UTC_year=wrf_file_time.year
        wrf_UTC_month=wrf_file_time.month
        wrf_UTC_day=wrf_file_time.day
        wrf_UTC_hour=wrf_file_time.hour
    except:
        wrf_file_time='None'
        print('time clips error')
        break
    #判断是否过了昨天的20点（北京时间，对应UTC时间12点）
    if (wrf_file_time=='None'):
        pass
    elif (wrf_file_time<yesterday_UTC_12h):
        "'judging weather more than 1-day time"
        wrfout_files[item]=''

   
    if (wrf_UTC_hour==12) | (wrf_UTC_hour==0):
        "'only select 8:00&20:00(beijing time) as putin data'"
        pass
#    elif (wrf_UTC_hours!=12) & (wrf_UTC_hour!=0) & (wrf_UTC_hours==11) | (wrf_UTC_hour==23)
    else:
        wrfout_files[item]=''
#        print(item,'true')
#    else:
#        print(item,'pass')
#    print(wrf_UTC_hour)

"'remove the empty element'"
while '' in wrfout_files:
    wrfout_files.remove('')
#print(wrfout_files)


